package com.zxf.appliction.service;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zxf.appliction.dao.JedisClient;
import com.zxf.appliction.dao.UsersMapper;
import com.zxf.appliction.domain.Params;
import com.zxf.appliction.domain.Users;


@Service  
//默认将类中的所有函数纳入事务管理.  
@Transactional  
public class UserService2 {
	@Autowired
	private JedisClient jedisClient;

	//@Value("${REDIS_USER_KEY}")
	//@Value("${USER_BASE_INFO_KEY}")
	
	 
	    @Autowired  
	    private UsersMapper userMapper;
	    
	    
	    @Resource
	    protected RedisTemplate<Serializable, Serializable> redisTemplate;
	  
	  
	 public void insertUser(Users user) {  
	        //新增用户  
	        userMapper.insert(user);  
	 }

	public Users getId(int userid) {
		return userMapper.selectByPrimaryKey(userid);
	}
	public List getUserList() {
		// 用户列表
		return userMapper.getUserList();
	}

	public Map<String, Object> getUserList2() {
		Map<String, Object> userMap ;
		userMap= userMapper.getUserList2();
		return userMap;
	}

	public List getUserList3(int pagenum, int numbers) {
		List userMap;
		Params pams = new Params();
		pams.setPagenum(pagenum);
		pams.setPagesize(numbers);
		userMap= userMapper.getUserList3(pams);
		return userMap;
	}

	public int getusertotle() {
		// 用户总数
		return userMapper.gettotle();
	}

	public Users selectusers(Users users) {
		return userMapper.selectusers(users);
	}

	//添加用户到redis
	public boolean add(Users users) {
		
		return saveUser(users);
		
	}

	public boolean saveUser(final Users user) {
		//设置key过期时间，因为要设置过期时间，只能在key上设置，所以不能用hash
        redisTemplate.execute(new RedisCallback<Object>() {

            @Override
            public Object doInRedis(RedisConnection connection) throws DataAccessException {
                connection.set(redisTemplate.getStringSerializer().serialize("user.uid"+ user.getUserid()),
                               redisTemplate.getStringSerializer().serialize("user.name"+user.getUsername()));
                return null;
            }

			
        });
		return false;
    }
}
